package com.espressif.iot.db.greenrobot.daos;

import java.util.List;
import java.util.ArrayList;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;

import de.greenrobot.dao.AbstractDao;
import de.greenrobot.dao.Property;
import de.greenrobot.dao.internal.SqlUtils;
import de.greenrobot.dao.internal.DaoConfig;
import de.greenrobot.dao.query.Query;
import de.greenrobot.dao.query.QueryBuilder;

import com.espressif.iot.db.greenrobot.daos.GenericDataDB;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/**
 * DAO for table GENERIC_DATA_DB.
 */
public class GenericDataDBDao extends AbstractDao<GenericDataDB, Long>
{
    
    public static final String TABLENAME = "GENERIC_DATA_DB";
    
    /**
     * Properties of entity GenericDataDB.<br/>
     * Can be used for QueryBuilder and for referencing column names.
     */
    public static class Properties
    {
        public final static Property Id = new Property(0, Long.class, "id", true, "_id");
        
        public final static Property DeviceId = new Property(1, long.class, "deviceId", false, "DEVICE_ID");
        
        public final static Property Timestamp = new Property(2, long.class, "timestamp", false, "TIMESTAMP");
        
        public final static Property Data = new Property(3, String.class, "data", false, "DATA");
        
        public final static Property DirectoryId = new Property(4, long.class, "directoryId", false, "DIRECTORY_ID");
    };
    
    private DaoSession daoSession;
    
    private Query<GenericDataDB> genericDataDirectoryDB_DatasQuery;
    
    public GenericDataDBDao(DaoConfig config)
    {
        super(config);
    }
    
    public GenericDataDBDao(DaoConfig config, DaoSession daoSession)
    {
        super(config, daoSession);
        this.daoSession = daoSession;
    }
    
    /** Creates the underlying database table. */
    public static void createTable(SQLiteDatabase db, boolean ifNotExists)
    {
        String constraint = ifNotExists ? "IF NOT EXISTS " : "";
        db.execSQL("CREATE TABLE " + constraint + "'GENERIC_DATA_DB' (" + //
            "'_id' INTEGER PRIMARY KEY ," + // 0: id
            "'DEVICE_ID' INTEGER NOT NULL ," + // 1: deviceId
            "'TIMESTAMP' INTEGER NOT NULL ," + // 2: timestamp
            "'DATA' TEXT NOT NULL ," + // 3: data
            "'DIRECTORY_ID' INTEGER NOT NULL );"); // 4: directoryId
    }
    
    /** Drops the underlying database table. */
    public static void dropTable(SQLiteDatabase db, boolean ifExists)
    {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "'GENERIC_DATA_DB'";
        db.execSQL(sql);
    }
    
    /** @inheritdoc */
    @Override
    protected void bindValues(SQLiteStatement stmt, GenericDataDB entity)
    {
        stmt.clearBindings();
        
        Long id = entity.getId();
        if (id != null)
        {
            stmt.bindLong(1, id);
        }
        stmt.bindLong(2, entity.getDeviceId());
        stmt.bindLong(3, entity.getTimestamp());
        stmt.bindString(4, entity.getData());
        stmt.bindLong(5, entity.getDirectoryId());
    }
    
    @Override
    protected void attachEntity(GenericDataDB entity)
    {
        super.attachEntity(entity);
        entity.__setDaoSession(daoSession);
    }
    
    /** @inheritdoc */
    @Override
    public Long readKey(Cursor cursor, int offset)
    {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }
    
    /** @inheritdoc */
    @Override
    public GenericDataDB readEntity(Cursor cursor, int offset)
    {
        GenericDataDB entity = new GenericDataDB( //
            cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
            cursor.getLong(offset + 1), // deviceId
            cursor.getLong(offset + 2), // timestamp
            cursor.getString(offset + 3), // data
            cursor.getLong(offset + 4) // directoryId
            );
        return entity;
    }
    
    /** @inheritdoc */
    @Override
    public void readEntity(Cursor cursor, GenericDataDB entity, int offset)
    {
        entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setDeviceId(cursor.getLong(offset + 1));
        entity.setTimestamp(cursor.getLong(offset + 2));
        entity.setData(cursor.getString(offset + 3));
        entity.setDirectoryId(cursor.getLong(offset + 4));
    }
    
    /** @inheritdoc */
    @Override
    protected Long updateKeyAfterInsert(GenericDataDB entity, long rowId)
    {
        entity.setId(rowId);
        return rowId;
    }
    
    /** @inheritdoc */
    @Override
    public Long getKey(GenericDataDB entity)
    {
        if (entity != null)
        {
            return entity.getId();
        }
        else
        {
            return null;
        }
    }
    
    /** @inheritdoc */
    @Override
    protected boolean isEntityUpdateable()
    {
        return true;
    }
    
    /** Internal query to resolve the "datas" to-many relationship of GenericDataDirectoryDB. */
    public List<GenericDataDB> _queryGenericDataDirectoryDB_Datas(long directoryId)
    {
        synchronized (this)
        {
            if (genericDataDirectoryDB_DatasQuery == null)
            {
                QueryBuilder<GenericDataDB> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.DirectoryId.eq(null));
                genericDataDirectoryDB_DatasQuery = queryBuilder.build();
            }
        }
        Query<GenericDataDB> query = genericDataDirectoryDB_DatasQuery.forCurrentThread();
        query.setParameter(0, directoryId);
        return query.list();
    }
    
    private String selectDeep;
    
    protected String getSelectDeep()
    {
        if (selectDeep == null)
        {
            StringBuilder builder = new StringBuilder("SELECT ");
            SqlUtils.appendColumns(builder, "T", getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T0", daoSession.getGenericDataDirectoryDBDao().getAllColumns());
            builder.append(" FROM GENERIC_DATA_DB T");
            builder.append(" LEFT JOIN GENERIC_DATA_DIRECTORY_DB T0 ON T.'DIRECTORY_ID'=T0.'_id'");
            builder.append(' ');
            selectDeep = builder.toString();
        }
        return selectDeep;
    }
    
    protected GenericDataDB loadCurrentDeep(Cursor cursor, boolean lock)
    {
        GenericDataDB entity = loadCurrent(cursor, 0, lock);
        int offset = getAllColumns().length;
        
        GenericDataDirectoryDB genericDataDirectoryDB =
            loadCurrentOther(daoSession.getGenericDataDirectoryDBDao(), cursor, offset);
        if (genericDataDirectoryDB != null)
        {
            entity.setGenericDataDirectoryDB(genericDataDirectoryDB);
        }
        
        return entity;
    }
    
    public GenericDataDB loadDeep(Long key)
    {
        assertSinglePk();
        if (key == null)
        {
            return null;
        }
        
        StringBuilder builder = new StringBuilder(getSelectDeep());
        builder.append("WHERE ");
        SqlUtils.appendColumnsEqValue(builder, "T", getPkColumns());
        String sql = builder.toString();
        
        String[] keyArray = new String[] {key.toString()};
        Cursor cursor = db.rawQuery(sql, keyArray);
        
        try
        {
            boolean available = cursor.moveToFirst();
            if (!available)
            {
                return null;
            }
            else if (!cursor.isLast())
            {
                throw new IllegalStateException("Expected unique result, but count was " + cursor.getCount());
            }
            return loadCurrentDeep(cursor, true);
        }
        finally
        {
            cursor.close();
        }
    }
    
    /** Reads all available rows from the given cursor and returns a list of new ImageTO objects. */
    public List<GenericDataDB> loadAllDeepFromCursor(Cursor cursor)
    {
        int count = cursor.getCount();
        List<GenericDataDB> list = new ArrayList<GenericDataDB>(count);
        
        if (cursor.moveToFirst())
        {
            if (identityScope != null)
            {
                identityScope.lock();
                identityScope.reserveRoom(count);
            }
            try
            {
                do
                {
                    list.add(loadCurrentDeep(cursor, false));
                } while (cursor.moveToNext());
            }
            finally
            {
                if (identityScope != null)
                {
                    identityScope.unlock();
                }
            }
        }
        return list;
    }
    
    protected List<GenericDataDB> loadDeepAllAndCloseCursor(Cursor cursor)
    {
        try
        {
            return loadAllDeepFromCursor(cursor);
        }
        finally
        {
            cursor.close();
        }
    }
    
    /** A raw-style query where you can pass any WHERE clause and arguments. */
    public List<GenericDataDB> queryDeep(String where, String... selectionArg)
    {
        Cursor cursor = db.rawQuery(getSelectDeep() + where, selectionArg);
        return loadDeepAllAndCloseCursor(cursor);
    }
    
}
